<template>
  <div>
  <el-card  style="height: 500px">
    <el-form label-width="100px" :rule="rules" :model="ruleForm" ref="ruleForm">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input placeholder="请输入原密码" type="password"  style="width: 200px" autocomplete="off" v-model="ruleForm.oldPassword"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="newPassword">
        <el-input placeholder="请输入新密码"  type="password" style="width: 200px" autocomplete="off" v-model="ruleForm.newPassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword">
        <el-input placeholder="请再次确认密码" type="password" style="width: 200px" autocomplete="off" v-model="ruleForm.checkPassword"></el-input>
      </el-form-item>
      <el-button type="primary" style="margin-left: 80px" @click="submitForm('ruleForm')">修改密码</el-button>
    </el-form>
  </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        id:localStorage.getItem("id"),
        oldPassword: '',
        newPassword: '',
        checkPassword:'',
      },
      rules: {
        newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
        ],
        checkPassword:[
          { required: true, message: '请确认密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url ="http://localhost:8080/v1/users/changePassword"
          console.log('url ='+url);
          let formDate =this.qs.stringify(this.ruleForm);
          console.log('formDate ='+formDate);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
              .post(url,formDate).then((response)=>{
                if (response.data.code==1){
                  this.$message.success('修改成功!')
                  //发请求退出登录
                  this.axios.post("http://localhost:8080/v1/users/logout").then(()=> {
                    localStorage.clear();//清空登录成功时保存的数据
                    location.href="/login";
                  })
                }else {

                  this.$message.error(response.data.msg);

                }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}

</script>

<style scoped>

</style>